package org.example.nettyrpc;

import org.example.nettyrpc.common.IpPortTuple;
import org.junit.Test;

public class RpcTest {
    private void RunEndpointA() throws Exception {
        SimpleEndPoint simpleEndPoint = new SimpleEndPoint(new IpPortTuple("127.0.0.1", 5656),
                new IpPortTuple[]{new IpPortTuple("127.0.0.1", 5657)}
        );
        simpleEndPoint.bootHostAndGuestRpc();
        System.out.println(simpleEndPoint.remoteCall(0, "speak"));
        System.out.println(simpleEndPoint.remoteCall(0, "add", 1, 3));
    }

    private void RunEndpointB() throws Exception {
        SimpleEndPoint simpleEndPoint = new SimpleEndPoint(new IpPortTuple("127.0.0.1", 5657),
                new IpPortTuple[]{new IpPortTuple("127.0.0.1", 5656)}
        );
        simpleEndPoint.bootHostAndGuestRpc();
    }

    @Test
    public void test001() throws InterruptedException {
        Thread t = new Thread(() -> {
            try {
                RunEndpointA();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
        t.start();

        new Thread(() -> {
            try {
                RunEndpointB();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }).start();

        t.join();
    }
}
